/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import Agency.views.*;
import Agency.models.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
/**
 *
 * @author wangari
 */
public class mainController  extends javax.swing.JFrame{
    private AllTenantsController allTheTenants;
    private DeleteTenantModel deleteTheTenant;
    private EditTenantInformation editTheTenant;
    private LoginController theLoginView;
    private NewTenantModel theNewTenant;
    private TenantsView   theTenantView;
    private HousesManagementview managementView;
    private NewHouseModel newHouse;
    private DeleteHouseModel deleteHouse;
    private AvailableHousesController availableHouses;
    private SearchTenantModel  theSearcher;
    
    Connection conn=null;
    private ActionListener actionlistener;
    /**
     * Constructor that takes models and views as parameters.
     * @param allTheTenants
     * @param deleteTheTenant
     * @param editTheTenant
     * @param theLoginView
     * @param theNewTenant
     * @param theTenantView
     * @param managementView
     * @param newHouse
     * @param deleteHouse
     * @param availableHouses
     * @param theSearcher 
     */
    public mainController(AllTenantsController allTheTenants,DeleteTenantModel deleteTheTenant,EditTenantInformation editTheTenant,LoginController theLoginView,
                       NewTenantModel theNewTenant,TenantsView theTenantView,HousesManagementview managementView,NewHouseModel  newHouse,
                       DeleteHouseModel deleteHouse,AvailableHousesController availableHouses,SearchTenantModel  theSearcher ){
        this.allTheTenants=allTheTenants;
        this.deleteTheTenant=deleteTheTenant;
        this.editTheTenant=editTheTenant;
        this.theLoginView=theLoginView;
        this.theNewTenant=theNewTenant;
        this.theTenantView=theTenantView;
        this.managementView=managementView;
        this.newHouse= newHouse;
        this.deleteHouse=deleteHouse;
        this.availableHouses=availableHouses;
        this.theSearcher= theSearcher;
    }
    
    /**
     * The main controller that controllers all the models.
     */
    public void theAgencyController(){
        //theLoginView.setVisible(true);
        
        actionlistener= new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource()== theLoginView.myLoginView.login()){
                    theLoginView.loginController(theTenantView);
//                   theTenantView.setVisible(true); 
                }
                else if(e.getSource()==theLoginView.myLoginView.logout()){
                   theLoginView.loggingOut();
                }
                else if (e.getSource()== theTenantView.manageHouses()){
                    managementView.setVisible(true);
                }
                else if(e.getSource()==theTenantView.searchTenant()){
                  theSearcher.mySearch.setVisible(true);  
                }
                else if(e.getSource()==theTenantView.closeTheView() ){
                    theTenantView.setVisible(false);
                }
                else if(e.getSource()==managementView.addHouse()){
                   newHouse.myHouse.setVisible(true); 
                }
                 
                else if (e.getSource()==managementView.deleteHouse()){
                  deleteHouse.houseToDelete.setVisible(true);  
                }
                else if(e.getSource()== managementView.availableHouses()){
                 availableHouses.allHouses.setVisible(true);   
                    
                }
                else if(e.getSource()==managementView.addNewHouse()){
                   newHouse.myHouse.setVisible(true); 
                }
                 else if (e.getSource()==managementView.deleteTheHouse()){
                  deleteHouse.houseToDelete.setVisible(true);  
                }
                 else if(e.getSource()== managementView.showAvailableHouses()){
                 availableHouses.allHouses.setVisible(true);   
                    
                }
                 
                else if(e.getSource()==managementView.closeHousesView()){
                   managementView.setVisible(false); 
                }
                else if(e.getSource()==newHouse.myHouse.saveDetails()){
                 newHouse.newHouseController();   
                }
                else if(e.getSource()==newHouse.myHouse.close()){
                  newHouse.closeView();  
                }
                else if(e.getSource()==deleteHouse.houseToDelete.confirming() ){
                deleteHouse.houseToDelete.gettheList().removeAllElements();
               deleteHouse.confirmTheHouse(); 
                }
                else if(e.getSource()==deleteHouse.houseToDelete.delete()){
                    deleteHouse.houseToDelete.gettheList().removeAllElements();
                deleteHouse.deleteTheHouse(); 
                }
                else if(e.getSource()==deleteHouse.houseToDelete.closeView()){
                 deleteHouse.houseToDelete.gettheList().removeAllElements();
                deleteHouse.closePage(); 
                }
                else if(e.getSource()==theNewTenant.myNewTenant.saveTenant()){
                   theNewTenant.AddNewTennant();
                }
                else if(e.getSource()==theNewTenant.myNewTenant.closeTenantView()){
                    theNewTenant.closeView();
                }
                else if(e.getSource()== theTenantView.addTenant()){
                    theNewTenant.myNewTenant.setVisible(true);
                }
                else if(e.getSource()== theTenantView.deleteTenant()){
                    deleteTheTenant.myDeleteView.setVisible(true);
                }
                else if(e.getSource()== theTenantView.editTenant()){
                    editTheTenant.myTenantEditor.setVisible(true);
                }
                else if(e.getSource()== theTenantView.viewAllTenants()){
                   allTheTenants.myTenantsView.setVisible(true);   
                }
                else if(e.getSource()== deleteTheTenant.myDeleteView.searchTenant()){
                    deleteTheTenant.searchTenant();
                }
                else if(e.getSource()== deleteTheTenant.myDeleteView.deleteTenant()){
                    deleteTheTenant.deleteTenant();
                }
                else if(e.getSource()== deleteTheTenant.myDeleteView.close()){
                    deleteTheTenant.closeView();
                }
                else if(e.getSource()==editTheTenant.myTenantEditor.searchTenant()){
                    editTheTenant.searchTheTenant();
                }
                else if(e.getSource()==editTheTenant.myTenantEditor.saveChanges()){
                    editTheTenant.saveTheChanges();
                }
                else if(e.getSource()== editTheTenant.myTenantEditor.closeView()){
                    editTheTenant.closeTheView();
                }
                else if(e.getSource()== allTheTenants.myTenantsView.displayButton()){
                    allTheTenants.myTenantsView.myTenantsTable().setRowCount(0);
                    allTheTenants.displayAllTenants(); 
                }
                else if (e.getSource()== allTheTenants.myTenantsView.closeButton()){
                    allTheTenants.myTenantsView.myTenantsTable().setRowCount(0);
                    allTheTenants.closeView(); 
                }
                else if(e.getSource()==allTheTenants.myTenantsView.exportData() ){
                     allTheTenants.exportingDataToTextFile(); 
                }
                else if(e.getSource()==availableHouses.allHouses.getApartments()){
                    availableHouses.allHouses.getTheList().removeAllElements();
              availableHouses.getTheApartments();
                }
                else if(e.getSource()==availableHouses.allHouses.get3BedRoom()){
                 availableHouses.allHouses.getTheList().removeAllElements();
              availableHouses.get3bedrooms();
            }
             else if(e.getSource()==availableHouses.allHouses.get2BedRoom() ){
             availableHouses. allHouses.getTheList().removeAllElements();
           availableHouses. get2bedrooms(); 
                }
             else if(e.getSource()==availableHouses.allHouses.get1BedRoom()){
                 availableHouses. allHouses.getTheList().removeAllElements();
          availableHouses.  get1bedrooms(); 
             }
             else if(e.getSource()==availableHouses.allHouses.getBedSitter()){
              availableHouses.allHouses.getTheList().removeAllElements();
            availableHouses.getBedsitters();   
            } 
             else if(e.getSource()==availableHouses.allHouses.getSingleRoom()){
                availableHouses.allHouses.getTheList().removeAllElements();
           availableHouses.getSingleRooms();   
             }
             else if(e.getSource()==availableHouses.allHouses.closeView()){
                availableHouses.closeThePage(); 
             }
             else if (e.getSource()==theSearcher.mySearch.search()){
                 theSearcher.searchTenant();
             }
             else if(e.getSource()==theSearcher.mySearch.closeSearch()){
              theSearcher.closePage();  
             }
             
                
                   
            }
        };
      
      //the controllers for the login view
       theLoginView. myLoginView.login().addActionListener(actionlistener);
       theLoginView.myLoginView.logout().addActionListener(actionlistener);
       //controllers for adding a new tenant
       theNewTenant.myNewTenant.saveTenant().addActionListener(actionlistener);
       theNewTenant.myNewTenant.closeTenantView().addActionListener(actionlistener);
       //controllers to add,close,delete and edit tenant information
       theTenantView.addTenant().addActionListener(actionlistener);
       theTenantView.closeTheView().addActionListener(actionlistener);
       theTenantView.deleteTenant().addActionListener(actionlistener);
       theTenantView.editTenant().addActionListener(actionlistener);
       theTenantView.viewAllTenants().addActionListener(actionlistener);
       theTenantView.searchTenant().addActionListener(actionlistener);
       //controllers for deleting  the tenant
       deleteTheTenant.myDeleteView.searchTenant().addActionListener(actionlistener);
       deleteTheTenant.myDeleteView.deleteTenant().addActionListener(actionlistener);
       deleteTheTenant.myDeleteView.close().addActionListener(actionlistener);
       //controllers for edditing  the tenants information
       editTheTenant.myTenantEditor.searchTenant().addActionListener(actionlistener);
       editTheTenant.myTenantEditor.saveChanges().addActionListener(actionlistener);
       editTheTenant.myTenantEditor.closeView().addActionListener(actionlistener);
       //controllers for showing all the tenants
       allTheTenants.myTenantsView.displayButton().addActionListener(actionlistener);
       allTheTenants.myTenantsView.closeButton().addActionListener(actionlistener);
       allTheTenants.myTenantsView.exportData().addActionListener(actionlistener);
       
       theTenantView.manageHouses().addActionListener(actionlistener);
       // controller for managing the houses
       managementView.addHouse().addActionListener(actionlistener);
       managementView.addNewHouse().addActionListener(actionlistener);
       managementView.deleteHouse().addActionListener(actionlistener);
       managementView.deleteTheHouse().addActionListener(actionlistener);
       managementView.availableHouses().addActionListener(actionlistener);
       managementView.showAvailableHouses().addActionListener(actionlistener);
       managementView.closeHousesView().addActionListener(actionlistener);
       //controller for adding a new house
      newHouse.myHouse.saveDetails().addActionListener(actionlistener);
      newHouse.myHouse.close().addActionListener(actionlistener); 
      /**
       * deleting a house
       */
    deleteHouse.houseToDelete.confirming().addActionListener(actionlistener);
    deleteHouse.houseToDelete.delete().addActionListener(actionlistener);
    deleteHouse.houseToDelete.closeView().addActionListener(actionlistener);
    /**
     * Controllers for all the available houses
     */
 availableHouses. allHouses.getApartments().addActionListener(actionlistener);
 availableHouses.allHouses.get3BedRoom().addActionListener(actionlistener);
 availableHouses.allHouses.get2BedRoom().addActionListener(actionlistener);
 availableHouses.allHouses.get1BedRoom().addActionListener(actionlistener);
 availableHouses.allHouses.getBedSitter().addActionListener(actionlistener);
 availableHouses.allHouses.getSingleRoom().addActionListener(actionlistener);
 availableHouses.allHouses.closeView().addActionListener(actionlistener);
 /**
  * controller  for the  search model
  */
theSearcher.mySearch.search().addActionListener(actionlistener);
theSearcher.mySearch.closeSearch().addActionListener(actionlistener);            
       
       
       }
 /**
  * Main controller 
  * @param args 
  */   
    public static void main(String[]args){
     AllTenantsController  allTenants= new AllTenantsController();
    DeleteTenantModel deleting = new DeleteTenantModel();
    EditTenantInformation  editting = new EditTenantInformation();
    LoginController login= new LoginController() ;
    NewTenantModel newTenant=  new NewTenantModel(); 
    TenantsView   theView= new TenantsView();
    HousesManagementview managementView= new HousesManagementview();
    NewHouseModel newHouse= new NewHouseModel();
    DeleteHouseModel deleteHouse= new DeleteHouseModel();
    AvailableHousesController availableHouses= new AvailableHousesController();
    SearchTenantModel  theSearcher= new SearchTenantModel();
    
    mainController agencyController= new mainController(allTenants,deleting,editting,login,newTenant,theView,managementView,
    newHouse,deleteHouse,availableHouses,theSearcher);
    
    agencyController.theAgencyController();
        
    }
}


